package Interface.Windows;

import Controller.InputController;
import Controller.LoginController;
import javax.persistence.EntityManagerFactory;

/**
 *
 * @author Nicolas
 */
public class UserValidation extends javax.swing.JFrame {

    InputController validator;
    LoginController loginController;
    EntityManagerFactory emf;

    /**
     * Creates new form UserValidation
     */
    public UserValidation(EntityManagerFactory emf) {
        this.emf = emf;
        validator = new InputController();
        loginController = new LoginController(emf);
        initComponents();
        this.setTitle("SIRU - Autenticación");
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel6 = new javax.swing.JLabel();
        javax.swing.JPanel ForestaLogoJF = new javax.swing.JPanel();
        logoLabel = new javax.swing.JLabel();
        AutenticationDataJF = new javax.swing.JPanel();
        userNameLabel = new javax.swing.JLabel();
        passwordLabel = new javax.swing.JLabel();
        userNameTF = new javax.swing.JTextField();
        passwordPF = new javax.swing.JPasswordField();
        lowerPanel = new javax.swing.JPanel();
        companyLabel = new javax.swing.JLabel();
        roghtMiddleLabel = new javax.swing.JLabel();
        leftMiddleLabel = new javax.swing.JLabel();
        LoginButton = new javax.swing.JButton();

        jLabel6.setText("jLabel6");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Autenticación");
        setMaximumSize(new java.awt.Dimension(1280, 720));
        setResizable(false);

        logoLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Interface/Resources/Icono Foresta.png"))); // NOI18N
        ForestaLogoJF.add(logoLabel);

        userNameLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Interface/Resources/user.png"))); // NOI18N
        userNameLabel.setText("Nombre de Usuario");

        passwordLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Interface/Resources/password.png"))); // NOI18N
        passwordLabel.setText("Contraseña");

        userNameTF.setToolTipText("Escribir Nombre de Usuario");
        userNameTF.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                userNameTFKeyTyped(evt);
            }
        });

        passwordPF.setToolTipText("Escribir contraseña");
        passwordPF.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                passwordPFActionPerformed(evt);
            }
        });
        passwordPF.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                passwordPFFocusGained(evt);
            }
        });
        passwordPF.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                passwordPFKeyTyped(evt);
            }
        });

        javax.swing.GroupLayout AutenticationDataJFLayout = new javax.swing.GroupLayout(AutenticationDataJF);
        AutenticationDataJF.setLayout(AutenticationDataJFLayout);
        AutenticationDataJFLayout.setHorizontalGroup(
            AutenticationDataJFLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, AutenticationDataJFLayout.createSequentialGroup()
                .addContainerGap(36, Short.MAX_VALUE)
                .addGroup(AutenticationDataJFLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(passwordLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(userNameLabel, javax.swing.GroupLayout.Alignment.TRAILING))
                .addGap(18, 18, 18)
                .addGroup(AutenticationDataJFLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(userNameTF)
                    .addComponent(passwordPF, javax.swing.GroupLayout.PREFERRED_SIZE, 213, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(35, 35, 35))
        );
        AutenticationDataJFLayout.setVerticalGroup(
            AutenticationDataJFLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(AutenticationDataJFLayout.createSequentialGroup()
                .addContainerGap(23, Short.MAX_VALUE)
                .addGroup(AutenticationDataJFLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(userNameLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(userNameTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(AutenticationDataJFLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(passwordLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(passwordPF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        companyLabel.setText("Foresta 2012");

        javax.swing.GroupLayout lowerPanelLayout = new javax.swing.GroupLayout(lowerPanel);
        lowerPanel.setLayout(lowerPanelLayout);
        lowerPanelLayout.setHorizontalGroup(
            lowerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(lowerPanelLayout.createSequentialGroup()
                .addComponent(companyLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 283, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        lowerPanelLayout.setVerticalGroup(
            lowerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(companyLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 45, Short.MAX_VALUE)
        );

        roghtMiddleLabel.setToolTipText("Ingresar");
        roghtMiddleLabel.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));

        leftMiddleLabel.setToolTipText("Salir");

        LoginButton.setText("Ingresar");
        LoginButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                LoginButtonMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(ForestaLogoJF, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(AutenticationDataJF, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(lowerPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(leftMiddleLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(roghtMiddleLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(LoginButton)
                        .addGap(38, 38, 38))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(ForestaLogoJF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(AutenticationDataJF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(roghtMiddleLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(leftMiddleLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 4, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(LoginButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lowerPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void userNameTFKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_userNameTFKeyTyped
        validator.checkUsernameBR(evt);
    }//GEN-LAST:event_userNameTFKeyTyped

    private void passwordPFKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_passwordPFKeyTyped
        validator.checkPasswordBR(evt);
    }//GEN-LAST:event_passwordPFKeyTyped

    private void passwordPFFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_passwordPFFocusGained
        passwordPF.setText("");
    }//GEN-LAST:event_passwordPFFocusGained

    private void LoginButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_LoginButtonMouseClicked
        String password = String.valueOf(passwordPF.getPassword());
        int rol = loginController.validateLogin(userNameTF.getText(), password);
        if(rol != 5){
            MainWindow mw = new MainWindow(emf, rol);
            mw.setLocationRelativeTo(null);
            mw.setVisible(true);
            this.dispose();
        }
    }//GEN-LAST:event_LoginButtonMouseClicked

    private void passwordPFActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_passwordPFActionPerformed
        LoginButtonMouseClicked(null);
    }//GEN-LAST:event_passwordPFActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel AutenticationDataJF;
    private javax.swing.JButton LoginButton;
    private javax.swing.JLabel companyLabel;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel leftMiddleLabel;
    private javax.swing.JLabel logoLabel;
    private javax.swing.JPanel lowerPanel;
    private javax.swing.JLabel passwordLabel;
    private javax.swing.JPasswordField passwordPF;
    private javax.swing.JLabel roghtMiddleLabel;
    private javax.swing.JLabel userNameLabel;
    private javax.swing.JTextField userNameTF;
    // End of variables declaration//GEN-END:variables
}
